﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBLL;
using System.Data;
using System.Data.Common;
using MisDLL;

namespace MisBLL
{
    public class RacfDAO : BaseDAO,IRacf
    {

        //退货出库
        public void Insert(MisEntity.Rac entity, IList<MisEntity.Racf> list)
        {
            dbContext.Rac.InsertOnSubmit(entity);
            MisEntity.Supply model = dbContext.Supply.First<MisEntity.Supply>(item => item.SUPP_ID == entity.Supp_id);
            if (model.SUMJEBUY == null)
                model.SUMJEBUY = 0;
            model.SUMJEBUY = model.SUMJEBUY - entity.sumje;

            MisEntity.SYS0 Sys0 = dbContext.SYS0.First<MisEntity.SYS0>(item => item.F1 == '1');
            MisEntity.Goods GItem = null;
            foreach (MisEntity.Racf RItem in list)
            {
                Sys0.NO_RAC += 1;
                GItem = dbContext.Goods.First<MisEntity.Goods>(G => G.ID == RItem.ID);
                GItem.SL0 = GItem.SL0 - RItem.sl;
                GItem.SL1 = GItem.SL1 - RItem.sl;
                GItem.JE1 = GItem.JE1 - RItem.je;
                if (RItem.shop_id == "02")
                {
                    GItem.SL2 = GItem.SL2 - RItem.sl;
                }
                else if (RItem.shop_id == "03")
                {
                    GItem.SL3 = GItem.SL3 - RItem.sl;
                }

                if (RItem.SLKC <= 0)
                {
                    GItem.date_0 = DateTime.Now;
                }

                RItem.NONO = (decimal)Sys0.NO_RAC;
                RItem.PACK = (short)GItem.PACK;
                RItem.UNIT_P = GItem.UNIT_P;
                RItem.date = DateTime.Now;
                RItem.BAT = (short)GItem.BATS;
                RItem.SLKC = (decimal)GItem.SL1;
                RItem.JEKC = (decimal)GItem.JE1;
                RItem.DAY_JS = DateTime.Now;
                RItem.Jhtype = ' ';
                RItem.TYPE_JS = "";
                RItem.ce = 0;
                RItem.GOODS = GItem.GOODS;
                RItem.F0 = GItem.F0;

                dbContext.Racf.InsertOnSubmit(RItem);
            }

            dbContext.SubmitChanges();
        }

        //移库
        public void Move(IList<MisEntity.Racf> list, string Shop1, string Shop2)
        {
            MisEntity.SYS0 Sys0 = dbContext.SYS0.First<MisEntity.SYS0>(item => item.F1 == '1');
            MisEntity.Goods GItem = null;
            foreach (MisEntity.Racf RItem in list)
            {
                GItem = dbContext.Goods.First<MisEntity.Goods>(G => G.ID == RItem.ID);

                if (Shop1 == "02")
                {
                    GItem.SL2 = GItem.SL2 - RItem.sl;
                }
                else if (Shop1 == "03")
                {
                    GItem.SL3 = GItem.SL3 - RItem.sl;
                }

                if (Shop2 == "02")
                {
                    GItem.SL2 = GItem.SL2 + RItem.sl;
                }
                else if (Shop2 == "03")
                {
                    GItem.SL3 = GItem.SL3+ RItem.sl;
                }
                Sys0.NO_RAC += 1;
                RItem.NONO = (decimal)Sys0.NO_RAC;
                RItem.PACK = (short)GItem.PACK;
                RItem.UNIT_P = GItem.UNIT_P;
                RItem.date = DateTime.Now;
                RItem.BAT = (short)GItem.BATS;
                RItem.SLKC = (decimal)GItem.SL1;
                RItem.JEKC = (decimal)GItem.JE1;
                RItem.DAY_JS = DateTime.Now;
                RItem.TYPE_JS = "";
                RItem.ce = 0;
                RItem.GOODS = GItem.GOODS;
                RItem.F0 = GItem.F0;
                RItem.Supp_id = Shop2;
                RItem.shop_id = Shop2;
                dbContext.Racf.InsertOnSubmit(RItem);

                Sys0.NO_RAC += 1;
                RItem.NONO = (decimal)Sys0.NO_RAC;
                RItem.PACK = (short)GItem.PACK;
                RItem.UNIT_P = GItem.UNIT_P;
                RItem.date = DateTime.Now;
                RItem.BAT = (short)GItem.BATS;
                RItem.SLKC = (decimal)GItem.SL1;
                RItem.JEKC = (decimal)GItem.JE1;
                RItem.DAY_JS = DateTime.Now;
                RItem.TYPE_JS = "";
                RItem.ce = 0;
                RItem.GOODS = GItem.GOODS;
                RItem.F0 = GItem.F0;
                RItem.sl = -RItem.sl;
                RItem.je = -RItem.je;
                RItem.SL_P = -RItem.SL_P;
                RItem.Supp_id = Shop1;
                RItem.shop_id = Shop1;
                dbContext.Racf.InsertOnSubmit(RItem);
            }
            dbContext.SubmitChanges();
        }

        //获取退货查询数据
        public DataTable GetEnquiriesAll(string sql, Dictionary<string, object> dictionary)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("select bf.no,bf.date,bf.dep_id,bf.supp_id,s.supply,bf.jhtype,bf.id,g.goods,bf.sl,bf.je,bf.shop_id,g.price1,bf.ce,0,'1900.01.01',bf.cas,0,g.unit,bf.no_bei ");
            sb.Append("from racf_tab bf,goods_tab g,supply_tab s where bf.id=g.id and bf.supp_id=s.supp_id").Append(sql);
            DbCommand cmd = dbHelper.GetSqlStringCommond(sb.ToString());
            dbHelper.AddParameter(cmd, dictionary);
            return dbHelper.ExecuteDataTable(cmd);
        }

        //获取退货统计数据
        public DataTable GetStatisticsAll(string sql, Dictionary<string, object> dictionary, string tjType)
        {
            StringBuilder sb = new StringBuilder();
            if ("按商场".Equals(tjType))
            {
                sb.Append("select SUBSTRING(M.dep_id,1,2),D.DEPART,sum(M.SL),sum(m.sl*m.price),sum(M.JE) from RACF_tab M,DEPART_TAB D where");
                sb.Append(sql).Append(" AND SUBSTRING(M.dep_id,1,2)=D.DEP_ID GROUP BY SUBSTRING(M.DEP_ID,1,2),D.DEPART ORDER BY SUBSTRING(M.DEP_ID,1,2)");
            }
            else if ("按部门".Equals(tjType))
            {
                sb.Append("select SUBSTRING(M.dep_id,1,4),D.DEPART,sum(M.SL),sum(m.sl*m.price),sum(M.JE) from RACF_tab M,DEPART_TAB D where");
                sb.Append(sql).Append(" AND SUBSTRING(M.dep_id,1,4)=D.DEP_ID GROUP BY SUBSTRING(M.DEP_ID,1,4),D.DEPART order by SUBSTRING(M.DEP_ID,1,4)");
            }
            else if ("按合同号".Equals(tjType))
            {
                sb.Append("select m.con_no,m.dep_id,d.depart,sum(M.SL),sum(m.sl*m.price),sum(M.JE),m.supp_id,s.supply from RACF_tab M,supply_tab s,depart_tab d where");
                sb.Append(sql).Append(" and m.supp_id=s.supp_id and m.dep_id=d.dep_id GROUP BY m.con_no,m.dep_id,d.depart,m.supp_id,s.supply order by m.con_no");
            }
            else if ("按供应商".Equals(tjType))
            {
                sb.Append("select m.supp_id,s.supply,sum(M.SL),sum(m.sl*m.price),sum(M.JE) from RACF_tab M,supply_tab s where");
                sb.Append(sql).Append(" and m.supp_id=s.supp_id GROUP BY m.supp_id,s.supply order by m.supp_id");
            }
            else if ("按柜组".Equals(tjType))
            {
                sb.Append("select M.dep_id,D.DEPART,sum(M.SL),sum(m.sl*m.price),sum(M.JE) from RACF_tab M,DEPART_TAB D where");
                sb.Append(sql).Append(" AND M.DEP_ID=D.DEP_ID GROUP BY M.DEP_ID,D.DEPART order by m.dep_id");
            }
            else if ("按商品".Equals(tjType))
            {
                sb.Append("select M.id,D.goods,sum(M.SL),sum(m.sl*m.price),sum(M.JE) from RACF_tab M,goods_TAB D where");
                sb.Append(sql).Append(" AND M.id=D.ID GROUP BY M.ID,D.goods ORDER BY M.ID");
            }
            else
            {
                sb.Append("select M.dep_id,D.DEPART,sum(M.SL),sum(m.sl*m.price),sum(M.JE) from RACF_tab M,DEPART_TAB D where");
                sb.Append(sql).Append(" AND M.DEP_ID=D.DEP_ID GROUP BY M.DEP_ID,D.DEPART order by m.dep_id");
            }
            DbCommand cmd = dbHelper.GetSqlStringCommond(sb.ToString());
            dbHelper.AddParameter(cmd, dictionary);
            return dbHelper.ExecuteDataTable(cmd);
        }
    }
}
